package org.yx.service.sqcm.save_battery_group_formula;

import com.xiaoleilu.hutool.collection.CollUtil;
import com.xiaoleilu.hutool.util.RandomUtil;
import com.xiaoleilu.hutool.util.StrUtil;
import org.dbist.dml.Query;
import org.yx.exception.BizException;
import org.yx.models.SbasProdMat;
import org.yx.models.SqcmLveDat;
import org.yx.models.SqcmLveDef;
import org.yx.utils.BaseService;
import org.yx.utils.FieldMap;

import java.util.ArrayList;
import java.util.List;

/**
 * 保存电池的分档方案
 *
 * @author zxw
 * @since 2023/04/11
 **/
public class SqcmSaveBatteryGroupFormulaService extends BaseService {
    public SqcmSaveBatteryGroupFormulaOut saveBatteryGroupFormula(SqcmSaveBatteryGroupFormulaIn in) throws Exception {
        if (in.getLevelDef() == null) {
            throw new BizException("SYS-0004", FieldMap.of("parameter", "levelDef"));
        }
        if (CollUtil.isEmpty(in.getLevelDat())) {
            throw new BizException("SYS-0004", FieldMap.of("parameter", "levelDat"));
        }
        checkStringNotBlank(in.getLevelDef(), "matCode,levelVer");

        Query query = new Query();
        query.addFilter("orgCode", in.getUserOrgCode());
        query.addFilter("matCode", in.getLevelDef().getMatCode());
        query.addFilter("levelVer", in.getLevelDef().getLevelVer());
        SqcmLveDef levelDef = selectOne(SqcmLveDef.class, query);
        if (levelDef != null) {
            copyProperties(in.getLevelDef(), levelDef);
            update(levelDef);

            deleteList(SqcmLveDat.class, query);
        } else {
            levelDef = copyProperties(in.getLevelDef(), new SqcmLveDef());
            levelDef.setOrgCode(in.getUserOrgCode());

            query = new Query();
            query.addFilter("orgCode", in.getUserOrgCode());
            query.addFilter("matCode", in.getLevelDef().getMatCode());
            query.addFilter("matStatus", "N");
            if (selectSize(SbasProdMat.class, query) == 0) {
                throw new BizException("SBAS-0001", "产品${matCode}不存在或已经删除。", FieldMap.of("matCode", in.getLevelDef().getMatCode()));
            }
            insert(levelDef);
        }

        List<SqcmLveDat> levelDatList = new ArrayList<SqcmLveDat>();
        for(SqcmLveDat dat : in.getLevelDat()){
            dat.setOrgCode(levelDef.getOrgCode());
            dat.setId(RandomUtil.simpleUUID());
            dat.setMatCode(levelDef.getMatCode());
            dat.setLevelVer(levelDef.getLevelVer());
            dat.setStatus("N");
            if(StrUtil.isBlank(dat.getcFlag())){
                dat.setcFlag("N");
            }
            if(StrUtil.isBlank(dat.getvFlag())){
                dat.setvFlag("N");
            }
            if(StrUtil.isBlank(dat.getrFlag())){
                dat.setrFlag("N");
            }
            if(StrUtil.isBlank(dat.gettFlag())){
                dat.settFlag("N");
            }
            if(StrUtil.isBlank(dat.getSvFlag())){
                dat.setSvFlag("N");
            }
            if(StrUtil.isBlank(dat.getkFlag())){
                dat.setkFlag("N");
            }
            if(StrUtil.isBlank(dat.getaFlag())){
                dat.setaFlag("N");
            }

            levelDatList.add(dat);
        }

        insertBatch(levelDatList);

        SqcmSaveBatteryGroupFormulaOut out = new SqcmSaveBatteryGroupFormulaOut();

        return out;
    }
}